package com.sk.sourcecircle.adapter;

import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.entity.GroupCircleList;
import com.sk.sourcecircle.utils.MyUtils;

import java.util.List;

/**
 * Created by sk on 2018/1/8.
 */

public class GroupCircleAdapter extends BaseMultiItemQuickAdapter<GroupCircleList.DataBean, BaseViewHolder> {

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public GroupCircleAdapter(List<GroupCircleList.DataBean> data) {
        super(data);
        addItemType(0, R.layout.group_not_care_circle);
        addItemType(1, R.layout.group_care_circle);
    }

    @Override
    protected void convert(BaseViewHolder helper, GroupCircleList.DataBean item) {
        switch (item.getItemType()) {
            case 0:
                Glide.with(mContext).load(item.getHead_url()).into((ImageView) helper.getView(R.id.head_img));
                helper.setText(R.id.tv_name, item.getC_name())
                        .setText(R.id.group_pep, "群人数" + item.getFollowcount() + "人")
                        .setText(R.id.tv_content, "介绍 " + item.getDesc());
                helper.addOnClickListener(R.id.add_care);

                break;
            case 1:

                EMConversation conversation = EMClient.getInstance().chatManager().getConversation(item.getGroup_id(), MyUtils.getConversationType(2), true);
                conversation.getAllMessages();
                EMMessage message = conversation.getLastMessage();
                if (message != null) {
                    switch (message.getType()) {
                        case TXT:
                            EMTextMessageBody body = (EMTextMessageBody) message.getBody();
                            helper.setText(R.id.tv_content, "【" + conversation.getUnreadMsgCount() + "】条  " + conversation.getLastMessage().ext().get("nickname") + "：" + body.getMessage());
                            break;
                        case IMAGE:
                            helper.setText(R.id.tv_content, "【" + conversation.getUnreadMsgCount() + "】条  " + conversation.getLastMessage().ext().get("nickname") + "：[图片]");
                            break;
                        case VOICE:
                            helper.setText(R.id.tv_content, "【" + conversation.getUnreadMsgCount() + "】条  " + conversation.getLastMessage().ext().get("nickname") + "：[语音]");
                            break;
                        case VIDEO:
                            helper.setText(R.id.tv_content, "【" + conversation.getUnreadMsgCount() + "】条  " + conversation.getLastMessage().ext().get("nickname") + "：[视频]");
                            break;
                    }
                }
                Glide.with(mContext).load(item.getHead_url()).into((ImageView) helper.getView(R.id.head_img));
                helper.setText(R.id.tv_name, item.getC_name())
                        .setText(R.id.group_pep, "群人数" + item.getFollowcount());
                break;
        }
    }
}
